package com.hup.joystick.view.main;

import com.hup.joystick.model.FunctionConfig;
import com.hup.utils.commons.TextUtil;
import com.hup.utils.commons.hardware.MouseUtil;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.customView.ImageButton;
import com.hup.utils.swing.customView.ViewText;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.MenuElement;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 * @author hugan
 * @date 2020/10/23
 */
@Log4j2
public class FunctionPreviewItem extends JPanel {
    public JPanel root;
    public JCheckBox cbFunctionName;
    public ViewText txtApp;
    public JPanel pContent;
    public ImageButton btnUp;
    public ImageButton btnDown;
    public ViewText txtFunction;

    @Getter
    private FunctionConfig function;
    @Setter
    public PreviewEditListener previewEditListener;

    public FunctionPreviewItem() {
        $$$setupUI$$$();
        int fontSize = (int) (LookThemeUtil.getDefaultScaledFontSize() - 3 * LookThemeUtil.getScreenScale());
        Font font = new Font("宋体", Font.PLAIN, fontSize);
        txtApp.setFont(font);
        txtFunction.setFont(font);
        //log.info("fontSize={}", font.getSize());
        initListener();
    }

    private void initListener() {
        cbFunctionName.addActionListener(e -> {
            boolean enable = cbFunctionName.isSelected();
            updateEnable(enable);
            if (previewEditListener != null) {
                function.enable = enable;
                previewEditListener.onEditPreviewItem(function.name, enable);
            }
        });
        ActionListener al = e -> {
            if (previewEditListener != null)
                previewEditListener.onMove(FunctionPreviewItem.this, e.getSource() == btnUp);
        };
        btnUp.setActionListener(al);
        btnDown.setActionListener(al);

        //右键菜单
        JPopupMenu menu = new JPopupMenu();
        menu.add(new JMenuItem("编辑"));
        for (MenuElement item : menu.getSubElements()) {
            ((JMenuItem) item.getComponent()).addActionListener(e -> {
                if (previewEditListener != null)
                    previewEditListener.onShowFunctionSettingFrame(FunctionPreviewItem.this);
            });
        }
        MouseAdapter menuListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MouseUtil.isRightButton(e)) {
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        };
        JComponent[] components = {cbFunctionName, txtFunction, txtApp};
        for (JComponent component : components) {
            component.addMouseListener(menuListener);
        }
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        root.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(panel1, gbc);
        cbFunctionName = new JCheckBox();
        cbFunctionName.setText("配置名");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(cbFunctionName, gbc);
        btnUp = new ImageButton();
        btnUp.setIcon(new ImageIcon(getClass().getResource("/img/arrow_up.gif")));
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel1.add(btnUp, gbc);
        btnDown = new ImageButton();
        btnDown.setIcon(new ImageIcon(getClass().getResource("/img/arrow_down.gif")));
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        panel1.add(btnDown, gbc);
        pContent = new JPanel();
        pContent.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(pContent, gbc);
        txtApp = new ViewText();
        txtApp.setText("程序");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        pContent.add(txtApp, gbc);
        txtFunction = new ViewText();
        txtFunction.setText("功能");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        pContent.add(txtFunction, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root;
    }

    private void createUIComponents() {
        root = this;
    }

    public void setFunction(FunctionConfig function) {
        this.function = function;
        cbFunctionName.setSelected(function.enable);
        updateEnable(function.enable);
        txtApp.setVisible(!function.lstApp.isEmpty());
        if (!function.lstApp.isEmpty()) {
            txtApp.setText(TextUtil.getText(function.lstApp, (sb, hasNext, next) -> {
                sb.append(new File(next).getName());
                if (hasNext) sb.append('\n');
            }));
        }
        txtFunction.setVisible(!function.lstFunction.isEmpty());
        if (!function.lstFunction.isEmpty()) {
            txtFunction.setText(TextUtil.getText(function.lstFunction, (sb, hasNext, next) -> {
                next.getString(sb);
                if (hasNext) sb.append('\n');
            }));
        }
    }

    private void updateEnable(boolean enable) {
        pContent.setVisible(enable);
        if (enable) {
            cbFunctionName.setText(!function.exclusive ? function.name : (function.name + "(独占)"));
        } else {
            cbFunctionName.setText(function.name + "(禁用)");
        }
    }

    @Override
    public String toString() {
        return "FunctionPreviewItem{"
                + "function=" + (function == null ? "null" : function.name)
                + '}';
    }

    public interface PreviewEditListener {
        void onEditPreviewItem(String functionName, boolean enable);

        void onMove(FunctionPreviewItem item, boolean isUp);

        void onShowFunctionSettingFrame(FunctionPreviewItem item);
    }

}
